<template>
  <div>
    <!--<home-header :city="city"></home-header>-->
    <home-header></home-header>
    <home-swiper :list="swiperList"></home-swiper>
    <home-icons :list="iconList"></home-icons>
    <home-recommed :list="recommendList"></home-recommed>
    <home-weekend :list="weekendList"></home-weekend>
  </div>
</template>

<script>
  import HomeHeader from './components/Header.vue'
  import HomeSwiper from './components/Swiper.vue'
  import HomeIcons from './components/Icons.vue'
  import HomeRecommed from './components/Recommend.vue'
  import HomeWeekend from './components/Weekend.vue'
  import axios from 'axios'
  import {mapState} from 'vuex'

  export default {
    name: 'Home',
    components: {
      HomeHeader,
      HomeSwiper,
      HomeIcons,
      HomeRecommed,
      HomeWeekend
    },
    computed: {
      ...mapState(['city'])
    },
    data() {
      return {
        //city: '',
        swiperList: [],
        iconList: [],
        recommendList: [],
        weekendList: [],
        lastcity: ''
      }
    },
    methods: {
      // 获取首页数据
      getHomeInfo() {
        axios.get('/api/index.json?city=' + this.city).then(this.getHomeInfoSuccess)
      },
      getHomeInfoSuccess(res) {
        res = res.data
        // 如果请求成功，并且有数据
        if (res.ret && res.data) {
          const data = res.data
          console.log(data);
          //this.city = data.city
          this.swiperList = data.swiperList
          this.iconList = data.iconList
          this.recommendList = data.recommendList
          this.weekendList = data.weekendList
        }
      }
    },
    mounted() {
      console.log('mounted');
      // 将上次的数据保存
      this.lastcity = this.city
      // 获取首页数据
      this.getHomeInfo()
    },
    // 页面被重新显示的时候
    activated() {
      console.log('activated');
      if (this.lastcity !== this.city) {
        this.lastcity = this.city
        this.getHomeInfo();
      }
    }
  }
</script>

<style lang="stylus" scoped>

</style>
